package fei.yuzhi.com.loveflowers.fragment.classifyFragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.bean.ClassifyAllBean;
import fei.yuzhi.com.loveflowers.bean.ClassifyItemBean;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.LoginActivity;
import fei.yuzhi.com.loveflowers.ui.ProductActivity.ProductActivity;
import fei.yuzhi.com.loveflowers.utils.MyGridView;
import fei.yuzhi.com.loveflowers.zxing.android.CaptureActivity;
import fei.yuzhi.com.loveflowers.zxing.view.ViewfinderView;


/**
 * Created by Administrator on 2016/8/9.
 */
public class ClassifyFragmnt extends Fragment implements ClassifyContract.ClassifyView {
    private ListView lv_classify;
    private ClassifyPresenter presenter;
    private MyGridView gv_classifycontainer;
    private ClassifyItemAdapter itemadapter;
    private  ClassifyListAdapter alladapter;
    private ClassifyListLeftAdapter adapter;
    private ImageView classify_fragment_scan,iv_classify;
    private int key=0;
    private List<ClassifyAllBean.DatasBean> list1=new ArrayList<>();
    private List<ClassifyItemBean.DatasBean.ClassListBean.ChildBean> list2=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify, container, false);
        lv_classify= (ListView) view.findViewById(R.id.lv_classify);
        gv_classifycontainer= (MyGridView) view.findViewById(R.id.gv_classifycontainer);
        classify_fragment_scan= (ImageView) view.findViewById(R.id.classify_fragment_scan);
        iv_classify= (ImageView) view.findViewById(R.id.iv_classify);
        init();
        presenter=new ClassifyPresenter(2178,this);
        presenter.Connected();
        presenter.ConnectedAll();
        return view;
    }

    private void init() {
        classify_fragment_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), CaptureActivity.class);
                startActivity(intent);
            }
        });
        iv_classify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onGetClassifyAllBeanSuccess(List<ClassifyAllBean.DatasBean> list) {
//        拿到品牌数据
           alladapter=new ClassifyListAdapter(getActivity(),list);
           list1=list;
        gv_classifycontainer.setAdapter(alladapter);

    }

    @Override
    public void onGetClassifyAllBeanFail() {

    }

    @Override
    public void onGetClassifyItemBeanSuccess(final List<ClassifyItemBean.DatasBean.ClassListBean.ChildBean> list) {
//      拿到每个item对应的数据
        list2=list;
        itemadapter=new ClassifyItemAdapter(list,getActivity());
        gv_classifycontainer.setAdapter(itemadapter);

    }

    @Override
    public void onGetClassifyItemBeanFail() {

    }

    @Override
    public void onGetClassifySuccess(List<String> list) {
//        左侧列表
         adapter=new ClassifyListLeftAdapter(list,getActivity());
         lv_classify.setAdapter(adapter);
//        左侧listview监听
        lv_classify.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.changeSelected(position);
                adapter.notifyDataSetChanged();
                if(position==0){
                    gv_classifycontainer.setAdapter(alladapter);
                    gv_classifycontainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getActivity(),ProductActivity.class);
                            intent.putExtra("keyword",list1.get(position).getBrandId());
                            startActivity(intent);
                        }
                    });
                }else {
                    switch (position){
                        case 1:
                            key=2178;
                            break;
                        case 2:
                            key=2176;
                            break;
                        case 3:
                            key=2179;
                            break;
                        case 4:
                            key=2174;
                            break;
                        case 5:
                            key=2180;
                            break;
                        case 6:
                            key=2171;
                            break;
                        case 7:
                            key=2177;
                            break;
                    }
                    presenter=new ClassifyPresenter(key,ClassifyFragmnt.this);
                    presenter.ConnectedClassifyItem();
                    gv_classifycontainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getActivity(),ProductActivity.class);
                            intent.putExtra("keyword",list2.get(position).getGcId());
                            startActivity(intent);
                        }
                    });
                }
            }
        });

    }

    @Override
    public void onGetClassifyFail() {

    }


}
